﻿@using TaskoMask.BuildingBlocks.Contracts.Dtos.Organizations
@inject OrganizationApiService OrganizationApiService

<EditForm Model="OrganizationModel" OnValidSubmit="HandleUpdateOrganization">

    <DataAnnotationsValidator />

    <div class="form-group">
        <InputText @bind-Value="OrganizationModel.Name" placeholder="Name" class="form-control"></InputText>
        <ValidationMessage For="()=>OrganizationModel.Name" class="text-danger"></ValidationMessage>
    </div>
    <div class="form-group">
        <InputText @bind-Value="OrganizationModel.Description" placeholder="Description" class="form-control"></InputText>
        <ValidationMessage For="()=>OrganizationModel.Description" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary float-left">Update</button>
        <button @onclick="HandleDeleteOrganization" class="btn btn-danger text-white float-right">Delete</button>
    </div>

</EditForm>

@code {

    #region Fields and Parameters



    /// <summary>
    /// Get an organization to update by its Id
    /// </summary>
    [Parameter]
    public string Id { get; set; }



    /// <summary>
    /// Form Model
    /// </summary>
    UpdateOrganizationDto OrganizationModel = new UpdateOrganizationDto();



    /// <summary>
    /// Access to current modal
    /// </summary>
    [CascadingParameter]
    BlazoredModalInstance ModalInstance { get; set; }



    #endregion Fields and Parameters


    #region Protected Methods


    /// <summary>
    ///
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        //if Id has value so we need to handle update
        if (!string.IsNullOrEmpty(Id))
            await PrepareModelToUpdate();
    }


    #endregion Protected Methods



    #region private Methods



    /// <summary>
    /// Get model by Id from API
    /// </summary>
    private async Task PrepareModelToUpdate()
    {
        var organizationResult = await OrganizationApiService.GetAsync(Id);

        if (organizationResult.IsSuccess)
        {
            OrganizationModel = new UpdateOrganizationDto
                {
                    Id = organizationResult.Value.Id,
                    Name = organizationResult.Value.Name,
                    Description = organizationResult.Value.Description,
                };

        }
        else
        {
            QueryResultHandler<GetOrganizationDto>.Init()
                .WithResult(organizationResult)
                .ShowErrorToast(ToastService)
                .CloseModal(ModalInstance);
        }
    }





    /// <summary>
    ///
    /// </summary>
    private async Task HandleUpdateOrganization()
    {
        var result = await OrganizationApiService.UpdateAsync(Id, OrganizationModel);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Organization_Updated)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService)
            .CloseModal(ModalInstance);
    }



    /// <summary>
    ///
    /// </summary>
    private async Task HandleDeleteOrganization()
    {
        var confirmationModal = ModalService.Show<Confirmation>("Deletion Warning!", new ModalOptions { Class = "blazored-modal w-75 w-md-50 bg-light-gray" });
        var confirmationModalResult = await confirmationModal.Result;

        if (confirmationModalResult.Cancelled)
            return;

        var result = await OrganizationApiService.DeleteAsync(Id);
        CommandResultHandler.Init()
     .WithResult(result)
     .WithComponentMessageType(MessageType.Organization_Deleted)
     .ShowErrorToast(ToastService)
     .PublishMessage(ComponentMessageService)
     .CloseModal(ModalInstance);
    }


    #endregion private Methods

}